package com.springboot_gradrecruit.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.springboot_gradrecruit.common.PageResponse;
import com.springboot_gradrecruit.common.R;
import com.springboot_gradrecruit.mapper.BulletinMapper;
import com.springboot_gradrecruit.model.dto.bulletin.AddBulletinRequest;
import com.springboot_gradrecruit.model.dto.bulletin.PageBulletinRequest;
import com.springboot_gradrecruit.model.dto.bulletin.UpdateBulletinRequest;
import com.springboot_gradrecruit.model.entity.Bulletin;
import com.springboot_gradrecruit.model.vo.bulletin.BulletinVO;
import com.springboot_gradrecruit.service.BulletinService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
* @author zr
* @description 针对表【bulletin】的数据库操作Service实现
* @createDate 2024-12-19 09:37:55
*/
@Service
public class BulletinServiceImpl extends ServiceImpl<BulletinMapper, Bulletin>
    implements BulletinService {

    @Resource
    private BulletinMapper bulletinMapper;

    @Override
    public Long addBulletin(AddBulletinRequest addBulletinRequest) {
        //1. 获取参数
        Bulletin bulletin = new Bulletin();
        bulletin.setBulletinTitle(addBulletinRequest.getBulletinTitle());
        bulletin.setBulletinText(addBulletinRequest.getBulletinText());
        bulletin.setBulletinAuthor(addBulletinRequest.getBulletinAuthor());

        //2. 插入数据
        int insert = bulletinMapper.insert(bulletin);
        return (long) insert;
    }

    @Override
    public PageResponse<BulletinVO> pageBulletin(PageBulletinRequest pageBulletinRequest) {
        //1.获取参数
        String searchName = pageBulletinRequest.getSearchName();

        //2.构建查询条件
        Page<Bulletin> page = pageBulletinRequest.toPageDefaultSortByUpdateTime();

        //3.执行查询
        Page<Bulletin> bulletinPage = lambdaQuery()
                .like(StrUtil.isNotBlank(searchName), Bulletin::getBulletinTitle, searchName)
                .page(page);
        return PageResponse.of(bulletinPage, BulletinVO.class);
    }

    @Override
    public boolean updateBulletinById(UpdateBulletinRequest updateBulletinRequest) {
        //1.获取参数
        Long bulletinId = updateBulletinRequest.getBulletinId();
        String bulletinTitle = updateBulletinRequest.getBulletinTitle();
        String bulletinText = updateBulletinRequest.getBulletinText();
        String bulletinAuthor = updateBulletinRequest.getBulletinAuthor();

        //2.更新公告信息
        LambdaUpdateWrapper<Bulletin> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Bulletin::getBulletinId, bulletinId)
                .set(StrUtil.isNotBlank(bulletinTitle), Bulletin::getBulletinTitle, bulletinTitle)
                .set(StrUtil.isNotBlank(bulletinText), Bulletin::getBulletinText, bulletinText)
                .set(StrUtil.isNotBlank(bulletinAuthor), Bulletin::getBulletinAuthor, bulletinAuthor);
        return bulletinMapper.update(null, updateWrapper)>0;
    }
}




